/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.console.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.model.domain.channel.Channel;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.cud.ChannelCUDService;
import com.us.usws.service.ro.ChannelROService;
import com.us.usws.support.uitls.JSONDataUtil;
import com.us.usws.support.uitls.SiteWebContext;
import com.us.usws.support.uitls.WebContext;

/**
 * Struts2 action
 * @author robot
 */
public class ChannelAction extends BaseAction {

	private static final long serialVersionUID = 2810967278261951048L;

	private Channel channel;

	@Inject
	private ChannelROService channelROService;

	@Inject
	private ChannelCUDService channelCUDService;

	private String uids;
	
	private String parentUid;
	
	private String[] videoUids;
	
	private int[] seqNums;
	
	private String isHot;

	/**
	 * ajax load the list of Channel
	 */
	public void ajaxLoadChannels() {
		getOut().println(JSONDataUtil.jsonStringfromObject(channelROService.getChannelTree()));
	}
	
	/**
	 * ajax load the list of Channel
	 */
	public void ajaxLoadChannelRelateCats() {
		getOut().println(JSONDataUtil.jsonStringfromObject(channelROService.getChannelRelateCatTree(channel.getRelateCat())));
	}

	/**
	 * ajax load channel level
	 */
	public void ajaxLoadChannelLevel() {
		String jsonString = "";
		String name = "/";
//		try {
//			name = new String("顶级频道".getBytes("gbk"), "utf-8");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
		if(StringUtils.isNotBlank(parentUid)) {
			if(!parentUid.equals("/")) {
				jsonString = JSONDataUtil.jsonStringfromObject(channelROService.getSubChannels("/"));
			} else {
			    jsonString = "{uid:'/', name:'"+name+"'}";
			}
		} else {
			Channel topChannel = new Channel();
			topChannel.setName(name);
			topChannel.setUid("/");
			List<Channel> channels = new ArrayList<Channel>();
			channels.add(topChannel);
			channels.addAll(channelROService.getSubChannels("/"));
			jsonString = JSONDataUtil.jsonStringfromObject(channels);
		}
		getOut().println(jsonString);
	}

	/**
	 * ajax update Channel
	 */
	public void ajaxUpdateChannel() {
		getResponse().setContentType("text/html;charset=UTF-8");
		WebContext wc = new SiteWebContext(getRequest(), getResponse());
		DOUser user = wc.getUser();
		if (user != null && channel != null) {
			Date now = new Date(System.currentTimeMillis());
			String uid = channel.getUid();
			String path = getRealPath();
			/*
			 * uid is not null, and update channel
			 */
			if (StringUtils.isNotBlank(uid)) {
				channel.setLastUpdByUid(user.getUid());
				channel.setLastUpdDate(now);
				String picture = channel.getPicture();
				if(StringUtils.isNotBlank(picture) && picture.contains("/")) {
					channel.setPicture(path+channel.getPicture());
				}
				channelCUDService.updateChannel(channel);
			} else {
				/*
				 * uid is null, and add channel
				 */
				channel.setCreateByUid(user.getUid());
				channel.setCreateDate(now);
				channel.setPicture(path+channel.getPicture());
				channelCUDService.addChannel(channel);
			}
			getOut().print(true);
		} else {
			getOut().print(false);
		}
	}

	/**
	 * ajax update Channel
	 */
	public void ajaxExchangeChannelPosition() {
		getOut().print(channelCUDService.exchangeChannelPosition(uids));
	}
	
	/**
	 * add channel video
	 */
	public void ajaxAddChannelVideo() {
		getResponse().setContentType("text/html;charset=UTF-8");
		WebContext wc = new SiteWebContext(getRequest(), getResponse());
		DOUser user = wc.getUser();
		channelCUDService.addChannelVideo(videoUids, uids, user.getUid());
	}
	
	/**
	 * delete channel video
	 */
	public void ajaxDeleteChannelVideo() {
		channelCUDService.deleteChannelVideo(videoUids, uids);
	}
	
	/**
	 * top channel video
	 */
	public void ajaxTopChannelVideo() {
		channelCUDService.topChannelVideo(videoUids, uids, seqNums);
	}
	
	/**
	 * recomment channel video
	 */
	public void ajaxRecommentChannelVideo() {
		channelCUDService.recommentChannelVideo(videoUids, uids, isHot);
	}
	
	public void ajaxDeleteChannel() {
		channelCUDService.deleteChannelMore(uids);
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public Channel getChannel() {
		return channel;
	}

	public String getUids() {
		return uids;
	}

	public void setUids(String uids) {
		this.uids = uids;
	}

	public String getParentUid() {
		return parentUid;
	}

	public void setParentUid(String parentUid) {
		this.parentUid = parentUid;
	}

	public String[] getVideoUids() {
		return videoUids;
	}

	public void setVideoUids(String[] videoUids) {
		this.videoUids = videoUids;
	}

	public int[] getSeqNums() {
		return seqNums;
	}

	public void setSeqNums(int[] seqNums) {
		this.seqNums = seqNums;
	}

	public String getIsHot() {
		return isHot;
	}

	public void setIsHot(String isHot) {
		this.isHot = isHot;
	}
}